using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GamePlayController : MonoBehaviour
{
	public GameObject systemPause;

	public GameObject systemWin;

	public GameObject HandTutorial;

	public GameObject DOG;

	public tk2dTextMesh texhMeshlevelPlayer;

	public tk2dTextMesh texhMeshTotalcoin;

	public tk2dTextMesh[] texhMeshWinLose;

	public tk2dTextMesh texhMeshnumberHealth;

	public tk2dTextMesh texhMeshnumberBigHeath;

	public tk2dSprite skill1;

	public tk2dSprite skill2;

	public tk2dTextMesh texhMeshStage;

	private int localBonus;

	private int Bonus;

	private int localStage;

	private int localTimeScale = 1;

	private int idBG;

	private int[] tienthuong = new int[60]
	{
		500,
		500,
		500,
		500,
		500,
		1000,
		1000,
		1500,
		1500,
		3000,
		1500,
		1500,
		2000,
		2000,
		2500,
		2500,
		2500,
		2500,
		2500,
		5000,
		3000,
		3000,
		3000,
		3000,
		3000,
		3000,
		3000,
		3000,
		3000,
		8000,
		3500,
		3500,
		3500,
		3500,
		3500,
		4000,
		4000,
		4000,
		4000,
		10000,
		4500,
		4500,
		5000,
		5000,
		5500,
		5500,
		6000,
		6500,
		7000,
		15000,
		8500,
		8500,
		8500,
		9000,
		9500,
		10000,
		11000,
		15000,
		20000,
		30000
	};

	public Transform pointBg;

	public Transform pointWeather;

	private string[] nameSkill1 = new string[10]
	{
		"BT_skill1_none",
		"BT_skill1_gay",
		"BT_skill1_vuot",
		"BT_skill1_daoto",
		"BT_skill1_kiem",
		"BT_skill1_daonho",
		"BT_skill1_cung",
		"BT_skill1_kich",
		"BT_skill1_axe",
		"BT_skill1_zed"
	};

	private string[] nameSkill2 = new string[10]
	{
		"BT_skill2_none",
		"BT_skill2_gay",
		"BT_skill2_vuot",
		"BT_skill2_daoto",
		"BT_skill2_kiem",
		"BT_skill2_daonho",
		"BT_skill2_cung",
		"BT_skill2_kich",
		"BT_skill2_axe",
		"BT_skill2_zed"
	};

	private readonly string[] gameobjectname = new string[8]
	{
		"Attack",
		"Skill1",
		"Skill2",
		"HelpHealth",
		"Pause",
		"replay",
		"Slide",
		"HelpBigHealth"
	};

	private GameObject AudioC;

	public AudioClip[] AudioClipClip;

	public SpawnEnemy _spawn;

	private int randomMusic;

	public Transform pointMid;

	private void Awake()
	{
		randomMusic = UnityEngine.Random.Range(0, 3);
		AudioC = GameObject.FindGameObjectWithTag("Audio");
		AudioC.GetComponent<AudioSource>().clip = AudioClipClip[randomMusic];
		AudioC.GetComponent<AudioSource>().Play();
		UniRate.Instance.OnUserAttemptToRate += OnUserAttemptToRate;
	}

	private void OnEnable()
	{
		AdsVideoController.RequestInterstitial();
		Time.timeScale = 1f;
		GameManager.AllEnemy.Clear();
		GameManager.AllBoom.Clear();
		GameManager.numberKill = 0;
		GameManager.TotalgoldStage = 0;
		GameManager.CoinBonusEnemyDie = 0;
		GameManager.hesotangdame = 1;
		GameManager.isBossDeath = false;
		GameManager.isWin = false;
	}

	private void OnDestroy()
	{
		UniRate.Instance.OnUserAttemptToRate -= OnUserAttemptToRate;
	}

	private void Start()
	{
		GameManager.numberRate++;
		skill1.SetSprite(nameSkill1[GameManager.indexVukhi]);
		skill2.SetSprite(nameSkill2[GameManager.indexVukhi]);
		if (GameManager.Dog > 0)
		{
			DOG.SetActive(value: true);
		}
		localStage = GameManager.stage;
		if (localStage < 61)
		{
			idBG = (localStage - 1) / 10;
			Bonus = tienthuong[localStage - 1];
			localBonus = Bonus;
			texhMeshStage.text = localStage.ToString();
		}
		if (localStage == 61)
		{
			idBG = UnityEngine.Random.Range(0, 6);
			texhMeshStage.text = "Endless";
		}
		RandomWeather();
		InstanceBackground();
		if (GameManager.Tutorial == 0)
		{
			GameObject.Find(gameobjectname[1]).GetComponent<Collider>().enabled = false;
			GameObject.Find(gameobjectname[2]).GetComponent<Collider>().enabled = false;
			GameObject.Find(gameobjectname[3]).GetComponent<Collider>().enabled = false;
			GameObject.Find(gameobjectname[4]).GetComponent<Collider>().enabled = false;
			GameObject.Find(gameobjectname[6]).GetComponent<Collider>().enabled = false;
		}
	}

	private void RandomWeather()
	{
		int num = UnityEngine.Random.Range(0, 4);
		if (num < 3)
		{
			GameObject gameObject = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("Weather/" + num));
			gameObject.transform.parent = pointWeather;
			gameObject.transform.localPosition = Vector3.zero;
		}
	}

	private void Update()
	{
		texhMeshlevelPlayer.text = GameManager.levelPlayer.ToString();
		texhMeshTotalcoin.text = GameManager.TotalCoin.ToString();
		texhMeshnumberHealth.text = GameManager.numberHealth.ToString();
		texhMeshnumberBigHeath.text = GameManager.numberBigHealth.ToString();
	}

	private void OnUserAttemptToRate()
	{
		GameManager.rate = 1;
	}

	private void InstanceBackground()
	{
		GameObject gameObject = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("BG/" + idBG));
		gameObject.transform.parent = pointBg;
		gameObject.transform.localPosition = Vector3.zero;
	}

	public void SinhStage1()
	{
		_spawn.InstanceEnemy();
	}

	public void Win()
	{
		StartCoroutine(StopGame());
	}

	public void Lose()
	{
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("systemLose")) as GameObject;
		gameObject.transform.parent = pointMid;
		gameObject.transform.localPosition = Vector3.zero;
	}

	public void OnclickPause()
	{
		systemPause.SetActive(value: true);
		Time.timeScale = 0f;
	}

	public void OnclickContinue()
	{
		systemPause.SetActive(value: false);
		Time.timeScale = localTimeScale;
	}

	public void OnclickMap()
	{
		SceneManager.LoadScene("SelectMap");
	}

	public void OnclickNext()
	{
		GameManager.stage = localStage + 1;
		SceneManager.LoadScene("GamePlay");
	}

	public void OnclickRestart()
	{
		GameManager.stage = localStage;
		SceneManager.LoadScene("GamePlay");
	}

	public void MyTimeScale(tk2dUIItem item)
	{
		if (Time.timeScale == 1f)
		{
			Time.timeScale = 1.3f;
			Time.timeScale = 1.3f;
			item.GetComponent<tk2dSprite>().SetSprite("x2");
		}
		else if (Time.timeScale == 1.3f)
		{
			Time.timeScale = 1f;
			localTimeScale = 1;
			item.GetComponent<tk2dSprite>().SetSprite("x1");
		}
	}

	private IEnumerator StopGame()
	{
		AudioController.Instance.SoundWinner();
		if (GameManager.Tutorial == 0)
		{
			GameManager.Tutorial++;
		}
		if (GameManager.stage < 60)
		{
			GameManager.stage++;
		}
		if (!GameManager.isHard)
		{
			if (GameManager.stage > GameManager.key_stage)
			{
				GameManager.key_stage = GameManager.stage;
			}
		}
		else if (GameManager.stage > GameManager.key_stageHard)
		{
			GameManager.key_stageHard = GameManager.stage;
		}
		yield return new WaitForSeconds(2.5f);
		SaveGameController.SaveGame();
		Time.timeScale = 1f;
		systemWin.SetActive(value: true);
		if (GameManager.rate < 1 && GameManager.numberRate % 3 == 0)
		{
			UniRate.Instance.PromptIfNetworkAvailable();
		}
		else if (GameManager.numberRate >= 3)
		{
			AdsVideoController.ShowAdFull();
		}
		texhMeshWinLose[0].text = GameManager.numberKill.ToString();
		texhMeshWinLose[0].Commit();
		texhMeshWinLose[1].text = GameManager.TotalgoldStage.ToString();
		texhMeshWinLose[1].Commit();
		if (!GameManager.isHard)
		{
			if (GameManager.BonusStage[GameManager.stage - 1] == -1)
			{
				GameManager.BonusStage[GameManager.stage - 1]++;
			}
			else
			{
				Bonus = localBonus / 5;
			}
		}
		else if (GameManager.BonusStageHard[GameManager.stage - 1] == -1)
		{
			GameManager.BonusStageHard[GameManager.stage - 1]++;
		}
		else
		{
			Bonus = localBonus / 5;
		}
		texhMeshWinLose[2].text = Bonus.ToString();
		texhMeshWinLose[2].Commit();
		GameManager.TotalCoin += Bonus;
		if (GameManager.Tutorial == 1)
		{
			GameObject.Find(gameobjectname[5]).GetComponent<Collider>().enabled = false;
			HandTutorial.SetActive(value: true);
		}
	}
}
